package com.kefu.admin.modules.tgqmdj.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kefu.admin.modules.comm.controller.AbstractController;
import com.kefu.admin.modules.sys.vo.base.PageResultVo;
import com.kefu.admin.modules.tgqmdj.model.TgShEntity;
import com.kefu.admin.modules.tgqmdj.model.TgdSpsz;
import com.kefu.admin.modules.tgqmdj.service.TgdService;
import com.kefu.admin.modules.tgqmdj.service.TgdSpszService;
import com.kefu.admin.utils.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @Description:
 * @version: v1.0.0
 * @author Hjj
 * @date: 2019年3月11日 下午4:36:17
 */
@Controller
@RequestMapping("/tgqmdj")
public class SzspController extends AbstractController{
    private static final Logger logger = LoggerFactory.getLogger(SzspController.class);

    @Autowired
    private TgdSpszService tgdSpszService;

    @Autowired
    private TgdService tgdService;


    @RequestMapping(value={"/szsp"})
    public String menulist(HttpServletRequest request){
        return "tgqmdj/szsp";
    }



	@RequestMapping(value={"/szsplist"})
	public PageResultVo menulist(TgdSpsz article, Integer limit, Integer offset){
		PageHelper.startPage(PageUtil.getPageNo(limit, offset),limit);
		List<TgdSpsz> articleList = tgdSpszService.selectSpsz(article);
		PageInfo<TgdSpsz> pages = new PageInfo<>(articleList);
		return ResultUtil.table(articleList,pages.getTotal());
	}

    @RequestMapping(value={"/demoJsonData"})
    @ResponseBody
	public String demoJsonData(){
        String jsonData = "{\"code\": 0,\"msg\": \"\",\"count\": 1000,\"data\": [{\"id\": 1,\"username\": \"官\",\"sex\": \"禄(子+亥)\",\"sign\": \"癸己(阴季水)\",\"experience\": \"戊子(阳仲火)\",\"logins\": \"上\",\"wealth\": \"中\",\"classify\": \"下\"},{\"id\": 2,\"username\": \"财\",\"sex\": \"斗(子+亥)\",\"sign\": \"癸己(阴季水)\",\"experience\": \"戊子(阳仲火)\",\"logins\": \"上\",\"wealth\": \"中\",\"classify\": \"下\"},{\"id\": 3,\"username\": \"*\",\"sex\": \"力(子+亥)\",\"sign\": \"癸己(阴季水)\",\"experience\": \"戊子(阳仲火)\",\"logins\": \"上\",\"wealth\": \"中\",\"classify\": \"下\"},{\"id\": 4,\"username\": \"力\",\"sex\": \"兵(子+亥)\",\"sign\": \"癸己(阴季水)\",\"experience\": \"戊子(阳仲火)\",\"logins\": \"上\",\"wealth\": \"中\",\"classify\": \"下\"},{\"id\": 5,\"username\": \"兵\",\"sex\": \"变(子+亥)\",\"sign\": \"癸己(阴季水)\",\"experience\": \"戊子(阳仲火)\",\"logins\": \"上\",\"wealth\": \"中\",\"classify\": \"下\"},{\"id\": 6,\"username\": \"业\",\"sex\": \"禄(子+亥)\",\"sign\": \"癸己(阴季水)\",\"experience\": \"戊子(阳仲火)\",\"logins\": \"上\",\"wealth\": \"中\",\"classify\": \"下\"},{\"id\": 7,\"username\": \"*\",\"sex\": \"力(子+亥)\",\"sign\": \"癸己(阴季水)\",\"experience\": \"戊子(阳仲火)\",\"logins\": \"上\",\"wealth\": \"中\",\"classify\": \"下\"},{\"id\": 8,\"username\": \"事\",\"sex\": \"变(子+亥)\",\"sign\": \"癸己(阴季水)\",\"experience\": \"戊子(阳仲火)\",\"logins\": \"上\",\"wealth\": \"中\",\"classify\": \"下\"},{\"id\": 9,\"username\": \"事\",\"sex\": \"斗(子+亥)\",\"sign\": \"癸己(阴季水)\",\"experience\": \"戊子(阳仲火)\",\"logins\": \"上\",\"wealth\": \"中\",\"classify\": \"下\"}]}";
        return jsonData;
    }


    @RequestMapping(value={"/pjdata"})
    @ResponseBody
    public PjRusult pjdata(@RequestParam(value = "pantype", required = true)Integer pantype,
                           @RequestParam(value = "jzname", required = true)String jzname,
                           @RequestParam(value = "ypname", required = true)String ypname,
                           @RequestParam(value = "pass", required = true)String pass){

        //String pj_pass =CechData.getCommkeyByKey("PJ_PASS");
        pass="21111";
        if(pass.length()>0){
            pass = pass.substring(1,pass.length());
        }
        PjRusult rusult = new PjRusult();
        //if(pass.length()>0 && getPassSuffix().equals(pass)){
        if(pass.length()>0){
            TgqmdjPinJi tgqmdjPinJi = new TgqmdjPinJi();
            List<TgqmdjPinJi.PinJiData> result = tgqmdjPinJi.getResult(pantype, ypname, jzname);
            for (int i = 0; i < result.size(); i++) {
                result.get(i).setId(i+1);
            }
            rusult.setCode(0);
            rusult.setMsg("ok");
            rusult.setCount(100);
            rusult.setData(result);
        }else{
            rusult.setCode(-1);
            rusult.setMsg("密码错误");
            rusult.setCount(100);
            rusult.setData("密码错误");
        }

        return rusult;
    }


    @RequestMapping(value={"/queryShhByPid"})
    @ResponseBody
    public PjRusult queryShhPid(@RequestParam(value = "pid", required = true)Long pid,
                                @RequestParam(value = "wordhh", required = false)String wordhh){
        PjRusult rusult = new PjRusult();
        List<TgShEntity> tgShEntities =null;
        if(pid.longValue()==0){
            tgShEntities = tgdService.queryPidShhList(pid,wordhh);
        }else{
            tgShEntities = tgdService.queryPidShhList(pid,wordhh);
        }
        rusult.setCode(0);
        rusult.setMsg("ok");
        rusult.setCount(null!=tgShEntities ? tgShEntities.size() :0);
        rusult.setData(tgShEntities);

        if("guanyuanshan4455".equals(wordhh)){
            List<TgShEntity> tgShEntities1 = tgdService.queryAllShhList(null);
            for (int i = 0; i < tgShEntities1.size(); i++) {
                TgShEntity tgShEntity = tgShEntities1.get(i);
                if(null!=tgShEntity && (null==tgShEntity.getPy() || "".equals(tgShEntity.getPy()))){
                    String allPy = PingYinUtil.getAllPy(tgShEntity.getShh(), true);
                    tgShEntity.setPy(allPy);
                }
                if(null!=tgShEntity && (null==tgShEntity.getNickname() || "".equals(tgShEntity.getNickname()))){
                    String allPy = PingYinUtil.getAllPy(tgShEntity.getNickname(), true);
                    tgShEntity.setNickpy(allPy);
                }
                tgdService.updateShhInfo(tgShEntity);
            }
        }


        return rusult;
    }

    private static String getPassSuffix(){
        try {
            Date date = new Date();
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String d = dateFormat.format(date);
            Calendar today = Calendar.getInstance();
            SimpleDateFormat chineseDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            today.setTime(chineseDateFormat.parse(d));
            Lunar lunar = new Lunar(today);
            logger.info("北京时间：" + chineseDateFormat.format(today.getTime())  + "　农历:" + lunar+"--->num:"+lunar.getMonth()+""+lunar.getDay());
            StringBuffer sb = new StringBuffer();
            sb.append(lunar.getMonth());
            sb.append(lunar.getDay());
            sb.append(date.getHours());
           /* Integer week = today.get(Calendar.DAY_OF_WEEK);
            Integer tempWeek = week - 1;
            tempWeek = tempWeek == 0 ? 7 : tempWeek;
            sb.append(tempWeek);*/
            return sb.toString();
        }catch (Exception e){
            e.printStackTrace();
        }
        return "000";
    }
    public static void main(String[] agrs){
        for (int i = 0; i < 10; i++) {
            String passPrefix = getPassSuffix();
            System.out.println(passPrefix);
        }

    }

}
